﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.UI;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.AContact
{
    public partial class EditContact01 : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request.QueryString["contactId"]))
                {
                    string contactId = Request.QueryString["contactId"];
                    LoadAllContactById(int.Parse(contactId, CultureInfo.CurrentCulture));
                }
                ViewState["filter"] = new AContactFilter();
                LoadAllBestContactMethod();
                LoadContactType();
            }
        }

        /// <summary>
        /// Loads the type of the contact.
        /// </summary>
        private void LoadContactType()
        {
            ICollection<ContactType> collection = AContactTypeBO.FindAllContactType();
            DropDownListContactType.DataSource = collection;
            DropDownListContactType.DataTextField = "ContactTypeName";
            DropDownListContactType.DataValueField = "ContactTypeId";
            DropDownListContactType.DataBind();
        }

        /// <summary>
        /// Loads all best contact method.
        /// </summary>
        private void LoadAllBestContactMethod()
        {
            ICollection<BestContactMethod> collection = ABestContactMethodBO.FindAllBestContactMethod();
            DropDownListBestMethod.DataSource = collection;
            DropDownListBestMethod.DataTextField = "BestContactMethodName";
            DropDownListBestMethod.DataValueField = "BestContactMethodId";
            DropDownListBestMethod.DataBind();
        }

        /// <summary>
        /// Loads all contact by id.
        /// </summary>
        /// <param name="contactId">The contact id.</param>
        private void LoadAllContactById(int contactId)
        {
            Contact objEntity = AContactBO.FindContactById(contactId);
            TextBoxFirstName.Text = objEntity.FirstName;
            TextBoxManagerName.Text = objEntity.FirstName + "" + objEntity.Surname;
            TextBoxSurname.Text = objEntity.Surname;
            DropDownListContactType.SelectedValue = objEntity.ContactTypeId.ToString(CultureInfo.CurrentCulture);
            TextBoxKnowAs.Text = objEntity.KnownAs;
            if (objEntity.BestContactMethodId != null)
                DropDownListBestMethod.SelectedValue =
                    objEntity.BestContactMethodId.Value.ToString(CultureInfo.CurrentCulture);
            TextBoxOfficePhone.Text = objEntity.OfficePhone;
            TextBoxJobTitle.Text = objEntity.JobTitle;
            TextBoxJobRole.Text = objEntity.JobRole;
            TextBoxMobilePhone.Text = objEntity.MobilePhone;
            TextBoxWorkBase.Text = objEntity.WorkBase;
            TextBoxHomePhone.Text = objEntity.HomePhone;
            TextBoxEmailAddress.Text = objEntity.Email;
            if (bool.Parse(objEntity.IsActive.ToString(CultureInfo.CurrentCulture)))
            {
                CheckBoxIsActive.Checked = true;
            }
            else
            {
                CheckBoxIsActive.Checked = false;
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            string firstName = TextBoxFirstName.Text;
            int managerName = 0;
            int contactId = 0;
            if (!String.IsNullOrEmpty(Request.QueryString["contactId"]))
            {
                managerName = int.Parse(Request.QueryString["contactId"], CultureInfo.CurrentCulture);
            }
            if (!String.IsNullOrEmpty(Request.QueryString["contactId"]))
            {
                contactId = int.Parse(Request.QueryString["contactId"], CultureInfo.CurrentCulture);
            }
            string Surname = TextBoxSurname.Text;
            int contactType = Convert.ToInt32(DropDownListContactType.SelectedValue,
                                              CultureInfo.CurrentCulture);
            string knowAs = TextBoxKnowAs.Text;
            int bestMethod = Convert.ToInt32(DropDownListBestMethod.SelectedValue,
                                             CultureInfo.CurrentCulture);
            string officePhone = TextBoxOfficePhone.Text;
            string mobilePhone = TextBoxMobilePhone.Text;
            string workBase = TextBoxWorkBase.Text;
            string homePhone = TextBoxHomePhone.Text;
            string jobTitle = TextBoxJobTitle.Text;
            string email = TextBoxEmailAddress.Text;
            string jobRole = TextBoxJobRole.Text;
            bool active;
            if (CheckBoxIsActive.Checked)
            {
                active = true;
            }
            else
            {
                active = false;
            }
            bool editData = AContactBO.EditContact(new Contact
                                                       {
                                                           ContactId = contactId,
                                                           FirstName = firstName,
                                                           BestContactMethodId = bestMethod,
                                                           ManageId = managerName,
                                                           Surname = Surname,
                                                           ContactTypeId = contactType,
                                                           KnownAs = knowAs,
                                                           OfficePhone = officePhone,
                                                           MobilePhone = mobilePhone,
                                                           WorkBase = workBase,
                                                           HomePhone = homePhone,
                                                           JobRole = jobRole,
                                                           JobTitle = jobTitle,
                                                           Email = email,
                                                           IsActive = active
                                                       });
            if (editData)
            {
                Response.Redirect("~/Contact_AnhTT12/ContactList.aspx");
            }
            else
            {
                Response.Write("<script>alert('Update Failed !');</script>");
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ContactList.aspx");
        }

        /// <summary>
        /// Handles the Click event of the LinkButtonLookup control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkButtonLookup_Click(object sender, EventArgs e)
        {
            ContactLookup.Popup();
        }
    }
}